package com.boost.tjvm.asm;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

public class AsmMethodVisit extends MethodVisitor {

    public AsmMethodVisit(MethodVisitor mv) {
        super(Opcodes.ASM7, mv);
    }


/*    LINENUMBER 27 L0
    ALOAD 0
    LDC "com.huawei.jaguar.cgw.CgwException"
    INVOKESTATIC cn/migu/millet/tools/ClassUtils.forName (Ljava/lang/String;)Ljava/lang/Class;
    PUTFIELD cn/migu/deal/cgw/wheat/transform/adaptor/CgwExceptionConverter.clz : Ljava/lang/Class;*/


    //此方法可以获取方法中每一条指令的操作类型，被访问多次
    //如应在方法结尾处添加新指令，则应判断：
    @Override
    public void visitInsn(int opcode) {

        /*if (opcode == Opcodes.INVOKESTATIC && mv.) {
            mv.visitFieldInsn(GETSTATIC,
                    "java/lang/System",
                    "out",
                    "Ljava/io/PrintStream;");
            mv.visitLdcInsn("this is a modify method!");
            mv.visitMethodInsn(INVOKEVIRTUAL,
                    "java/io/PrintStream",
                    "println",
                    "(Ljava/lang/String;)V");
        }*/
        super.visitInsn(opcode);
    }


    @Override
    public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) {

        if(owner.endsWith("java/lang/Integer") && name.equals("valueOf")){


            mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/boost/tjvm/remote/TestClass", "valueOf", descriptor, false);

            // 记载隐含的this对象，这是每个JAVA方法都有的
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            // 从常量池中加载“yy”字符到栈顶
            mv.visitLdcInsn("yy");
            // 将栈顶的"yy"赋值给name属性
            mv.visitFieldInsn(Opcodes.GETFIELD, owner, "i", descriptor);
        }

        super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
    }
}
